package com.kwlt.smc.dao;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.kwlt.smc.entity.Blacklist;

@Component
public class BlacklistExpDaoImpl implements BlacklistExpDao{
	
	@PersistenceContext 
	private EntityManager em;
	@Override
	public List<Blacklist> getBlacklistBySource(String source, String sourceName) {
		CriteriaBuilder builder = em.getCriteriaBuilder(); 
		CriteriaQuery<Blacklist> query = builder.createQuery(Blacklist.class); 
		Root<Blacklist> blacklist = query.from(Blacklist.class); 
		query.select(blacklist);
		
		List<Predicate> predicateList = new ArrayList<Predicate>(); 
		Predicate blacklistBatchNumPredicate; 
		//黑名单来源 1 运营商 2 通道 3 客户 4 其他
		if(!StringUtils.isBlank(source) && "1".equals(source) ){
			blacklistBatchNumPredicate = builder.equal(blacklist.get("source"),source);
			predicateList.add(blacklistBatchNumPredicate); 
			if(!StringUtils.isBlank(sourceName)){
				blacklistBatchNumPredicate = builder.equal(blacklist.get("operators"),sourceName);
				predicateList.add(blacklistBatchNumPredicate); 
			}
		}else if(!StringUtils.isBlank(source) && "2".equals(source) ){
			blacklistBatchNumPredicate = builder.equal(blacklist.get("source"),source);
			predicateList.add(blacklistBatchNumPredicate); 
			if(!StringUtils.isBlank(sourceName)){
				blacklistBatchNumPredicate = builder.equal(blacklist.get("channelCode"),sourceName);
				predicateList.add(blacklistBatchNumPredicate); 
			}
			
		}else if(!StringUtils.isBlank(source) && "3".equals(source)  ){
			blacklistBatchNumPredicate = builder.equal(blacklist.get("source"),source);
			predicateList.add(blacklistBatchNumPredicate); 
			if(!StringUtils.isBlank(sourceName)){
				blacklistBatchNumPredicate = builder.equal(blacklist.get("userCode"),sourceName);
				predicateList.add(blacklistBatchNumPredicate); 
			}
		}else if(!StringUtils.isBlank(source)){
			blacklistBatchNumPredicate = builder.equal(blacklist.get("source"),source);
			predicateList.add(blacklistBatchNumPredicate);
		}
		blacklistBatchNumPredicate = builder.notEqual(blacklist.get("status"), "X");
		predicateList.add(blacklistBatchNumPredicate);		
		Predicate[] predicates = new Predicate[predicateList.size()]; 
		predicateList.toArray(predicates); 
		query.where(predicates); 
		Order desc =null;
		if(!StringUtils.isBlank(source) && "1".equals(source) ){
			desc = builder.desc(blacklist.get("operators"));
		}else if(!StringUtils.isBlank(source) && "2".equals(source) ){
			desc = builder.desc(blacklist.get("channelCode"));
		}else if(!StringUtils.isBlank(source) && "3".equals(source)  ){
			 desc = builder.desc(blacklist.get("userCode"));
		}else{
			desc = builder.desc(blacklist.get("blacklistId"));
		}
		query.orderBy(desc);
		Query createQuery = em.createQuery(query);
		List resultList = createQuery.getResultList();
		return resultList; 
	}

}
